<?php


namespace app\api\model;


use app\common\model\BaseModel;
use common;
use hiredis;
use ApiOper;
use app\common\library\himongo;
use think\Db;


class NPAModel extends BaseModel
{
   protected $name = 'no_parking_area';
   protected $pk = 'id';

    function NO_PARKING_AREA_ID($city_id)
    {
        $key = 'NPA_ID|' . $city_id;

        $id = hiredis::db()->incr($key);
        if ($id == 1) {
            $where = [
                'city_id' => $city_id,
                'id' => ['<','a']
            ];
            $st = Db::name($this->name)->where($where)->max('id');


            if ($st === NULL) {
                $id = $city_id . "0001";
            } else {
                $id = strval(intval($st) + 1);
            }
            hiredis::db()->set($key, $id);
        }
        return strval($id);
    }

    var $_info = [];

    function add($info)
    {
        $paramlist = [
            'city_id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'coordinates' => 'array',
            'state' => 'int',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'type' => 'string',
            'is_no_park' => 'int',
        ];


        $data = common::type_conver($info, $paramlist);

        if (!$data) {
            $this->throw_myexception(X_ERROR, "service area info error!");
        }
        $data['center'] = common::polygon_center($data['coordinates']);
        $data['id'] = $this->NO_PARKING_AREA_ID($info['city_id']);
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['bike_count'] = 0;
        $data['tmp_id'] = $data['id'];

        ApiOper::instance()->append_creater_info($data);
        self::create($data);


        $this->save_to_mongo($data['city_id'], $data['id']);

        return $data;
    }

    function count($city_id, $condition)
    {
        $beans = [
            'city_id' => $city_id,
        ];

        if (isset($condition['state'])) {
            $beans['state'] = $condition['state'];
        }

        if (isset($condition['type'])) {
            $beans['type'] = $condition['type'];
        }

        if (isset($condition['start_time'])) {
            $beans['create_time'] = ['>=',$condition['start_time']];
        }
        if (isset($condition['end_time'])) {
            $beans['create_time'] = ['<=',$condition['end_time']];
        }
        if (!empty($condition['sk'])) {
            $beans['name'] = $condition['sk'];
        }


        return Db::name($this->name)->where($beans)->count('id');
    }

    function get_list($city_id, $condition, $pos, $limit)
    {
        $selectlist = [
            'id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'type' => 'string',
            'coordinates' => 'array',
            'state' => 'int',
            'creator_id' => 'string',
            'create_time' => 'string',
            'creator_name' => 'string',
            'updater_id' => 'string',
            'update_time' => 'string',
            'updater_name' => 'string',
            'bike_count' => 'int',
            'center' => 'array',
            'is_no_park' => 'int',
        ];

        $sfs = common::array_keys($selectlist);
        $beans = [
            'city_id'=>$city_id,
            'is_deleted' => 0
        ];

        if (isset($condition['state'])) {
            $beans['state'] = $condition['state'];
        }

        if (isset($condition['type'])) {
            $beans['type'] = $condition['type'];
        }

        if (isset($condition['start_time'])) {
            $beans['create_time'] = ['>=',$condition['start_time']];
        }
        if (isset($condition['end_time'])) {
            $beans['create_time'] = ['<=',$condition['end_time']];
        }
        if (!empty($condition['sk']) && !empty($condition['sk'])) {
            $sk = $condition['sk'];
            if (ctype_alnum($sk) && strlen($sk) == 14) {
                $beans['id'] = $sk;
            } else {
                $beans['name'] = ['like','%' . $condition['sk'] . '%'];
            }
        }

        $rows = $this->where($beans)->field($sfs)->limit($pos,$limit)->select();
        $l = [];
        foreach ($rows as $row) {
            $l[] = common::sql_row($selectlist, $row);
        }
        return $l;
    }

    function info($city_id, $id)
    {
        $selectlist = [
            'city_id' => 'string',
            'id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'type' => 'string',
            'coordinates' => 'array',
            'state' => 'int',
            'creator_id' => 'string',
            'create_time' => 'string',
            'creator_name' => 'string',
            'updater_id' => 'string',
            'update_time' => 'string',
            'updater_name' => 'string',
            'bike_count' => 'int',
            'is_deleted' => 'int',
            'center' => 'array',
            'is_no_park' => 'int',
            'tmp_id' => 'string',
        ];

        $sfs = common::array_keys($selectlist);
        $beans = [
            'city_id'=>$city_id,
            'id'=>$id
        ];


        return  $this->where($beans)->field($sfs)->find();
    }

    function update_npa($city_id, $id, $info)
    {
        $paramlist = [
            'name' => 'string',
            'remark' => 'string',
            'state' => 'int',
            'type' => 'string',
            'coordinates' => 'array',
            'is_no_park' => 'int',
            'tmp_id' => 'string',
        ];

        $data = common::type_conver($info, $paramlist);

        ApiOper::instance()->append_updater_info($data);
        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];

        self::update($data, $where);

        $this->sync_to_mongo($data,$where);
        return $data;
    }

    function delete_npa($city_id, $id)
    {
        $data = [
            'is_deleted' => 1,
            'update_time' => date('Y-m-d H:i:s'),
        ];

        ApiOper::instance()->append_updater_info($data);
        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];

        self::update($data, $where);

        $this->save_to_mongo($city_id, $id);
    }

    function incr_bike_count($city_id, $id, $total, $icrement)
    {
        if ($id == null || $city_id == null)
            return;
        if (empty($id) || empty($city_id))
            return;

        $beans = [
            'city_id'=>$city_id,
            'id'=>$id,
        ];


        Db::name($this->name)->where($beans)->setInc('bike',$icrement);

        $data = [
            'bike_count' => $total,
        ];
        $where = [
            'city_id' => $city_id,
            'id' => $id,
            'eftype' => EB_TYPE_NO_PARKING_AREA,
        ];

        himongo::db()->update($where, $data);
    }

    var $mongo_field = [
        'city_id' => 'string',
        'id' => 'string',
        'name' => 'string',
        'remark' => 'string',
        'type' => 'string',
        'coordinates' => 'array',
        'state' => 'int',
        'creator_id' => 'string',
        'create_time' => 'string',
        'creator_name' => 'string',
        'updater_id' => 'string',
        'update_time' => 'string',
        'updater_name' => 'string',
        'center' => 'array',
        'is_deleted' => 'int',
        'bike_count' => 'int',
        'is_no_park' => 'int',
        'tmp_id' => 'string',
    ];

    function save_to_mongo($city_id, $id)
    {
        $where = [
            'eftype' => EB_TYPE_NO_PARKING_AREA,
            'city_id' => $city_id,
            'id' => $id,
        ];
        himongo::db()->delete($where);

        $info = $this->info($city_id, $id);
        if ($info['state'] == 0 && $info['is_deleted'] == 0) {
            $data = common::type_conver($info, $this->mongo_field);
            $data['eftype'] = EB_TYPE_NO_PARKING_AREA;
            $data['polygon'] = [
                'type' => 'Polygon',
                'coordinates' => [$info['coordinates']],
            ];
            $data['point'] = [
                'type' => 'Point',
                'coordinates' => $data['center'],
            ];
            himongo::db()->insert($data);
        }
    }

    function sync_to_mongo($data, $where)
    {
        $where['eftype'] = EB_TYPE_NO_PARKING_AREA;
        $where['tmp_id'] = $where['id'];

        $info = common::type_conver($data, $this->mongo_field);
        if (empty($info))
            return;

        if (isset($info['coordinates'])) {
            $info['polygon'] = [
                'type' => 'Polygon',
                'coordinates' => [$info['coordinates']],
            ];
        }

        if (isset($info['center'])) {
            $info['point'] = [
                'type' => 'Point',
                'coordinates' => $info['center'],
            ];
        }

        himongo::db()->update($where, $info);
    }

    function is_in($city_id, $coordinate,$ptype =-1)
    {
        $condition = [
            'eftype' => EB_TYPE_NO_PARKING_AREA,
            'city_id' => $city_id,
        ];

        if ($ptype > 0){
            $condition['is_no_park'] = $ptype;
        }

        $result = himongo::db()->runconmand_intersects($coordinate, $condition, 10);

        foreach ($result as $item) {
            return $item;
        }
        return false;
    }

    function query_list_near($city_id, $coordinate)
    {
        $condition = [
            'eftype' => EB_TYPE_NO_PARKING_AREA,
            'state' => 0,
            'city_id' => $city_id,
        ];

        $projection = [
            'city_id' => 1,
            'id' => 1,
            'name' => 1,
            'type' => 1,
            'coordinates' => 1,
            'state' => 1,
            'center' => 1,
        ];
        $result = himongo::db()->near($coordinate[0], $coordinate[1], 3 * 1000, 1000, $condition, $projection);
        $l = [];
        foreach ($result as $item) {
            $l[] = common::type_conver($item, $this->mongo_field);
        }
        return $l;
    }

}